<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\SyncTaskRequest;
use App\CustHub\Service\ChSyncTaskService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 同步任务
 */
#[Controller('ch/syncTask'),ApiAuth,VerifySignature]
class SyncTaskController extends BaseController
{
    #[Inject]
    protected ChSyncTaskService $service;

    /**
     * 任务列表
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('list'),ChPermission('ch:sync:task:list')]
    public function list(): ResponseInterface
    {
        return $this->success($this->service->list($this->request->all()));
    }

    /**
     * 重新执行任务
     * @param SyncTaskRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('again')]
    public function again(SyncTaskRequest $request): ResponseInterface
    {
        return $this->service->again($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除任务
     * @param SyncTaskRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Exception
     */
    #[PostMapping('del')]
    public function del(SyncTaskRequest $request): ResponseInterface
    {
        return $this->service->del($request->all()) ? $this->success() : $this->error();
    }
}